
let localStorage = window.localStorage;
const localStorageKey = 'playground';

const objectParams = [
  'Width', 'Height', 'Length', 'Size', 'Position', 'Euler', 
  'Mass', 'Friction', 'RotationX', 'RotationY', 'RotationZ',
  'Speed', 'Restitution', 'Count', 'Rotation', 'Radius', 'Pressure',
  'Impulse'
];

const params = {
  // 保存
  saveParams:saveParams,
  clearParams:clearParams,
  // 背景
  sceneBackGroundColor: 0x1982cc,
  sceneLight:0x000000,
  cameraFov:60,
  cameraFar:2000,
  cameraNear:0.2,
  cameraPosition:"120,90,0",
  cameraLookAt:"0,0,0",
  cameraEuler:"0,0,0",

  // 跟随镜头尺寸
  followedWidth: 0.2,
  followedHeight: 0.2,
  panaromicOffset: 5,
  // 仿真
  simSpeed:1,
  simSpeeds:[0.1, 0.2, 0.5, 1, 2, 5],
  simStart:true,
  simPause:false,
  simReset:false,
  // 场地
  groundSize:128,
  // 开始平台
  startPlatformHeight:20,
  startPlatformWidth:5,
  startPlatformLength:3,
  startPlatformPosition:"-50, 10, -40",
  startPlatformEuler:"0, 0, 0",
  // 球杆
  poolCueSpeed:5,
  poolCueSize:1,
  poolCueMass:1,
  // 起始球
  startBallSize:1,
  startBallMass:1,
  startBallFriction:0.5,
  startBallRestitution:1,

  // 斜坡
  rampHeight:1,
  rampRotationZ:-30,

  // 水平碰撞桥
  hHitBridgeLength:8,
  hHitBridgeFriction:0.2,
  hHitBridgeRestitution:0.2,

  // 水平碰撞球
  hHitBallSize:1,
  hHitBallMass:1,
  hHitBallFriction:0.5,
  hHitBallRestitution:1,

  // 钟摆
  pendulumSize:1,
  pendulumHeight:20,
  pendulumMass:1,
  pendulumRestitution:1,

  // 滑板
  sliderHeight:10,
  sliderLength:20,
  sliderMass:1,
  sliderSize:1,
  sliderFriction:0.2,
  sliderRestitution:0.8,

  // 跷跷板
  seesawLength:20,
  seesawHeight:5,
  seesawMass:2,
  seesawFriction:0.2,
  seesawRestitution:0.8,

  // 天平
  balanceLength:20,
  balanceHeight:20,
  balanceMass:2,
  balanceSize:1,
  balanceFriction:0.8,
  balanceRestitution:0.8,

  // 漏斗
  hopperSize:1.2,
  hopperHeight: 5,
  hopperRotation:60,

  // 小车
  vehicleLength: 12,
  vehicleWidth: 8,
  vehicleHeight: 4,
  vehicleFriction:0.5,
  vehicleRestitution: 0.8,
  vehicleMass: 5,
  vehicleSpeed: 20,
  
  // 拱桥
  bridgeLength: 30,
  bridgeHeight: 20,
  bridgeFriction: 0.5,

  // 升降梯
  lifterHeight:20,
  lifterSpeed:5,

  // 推土机
  bulldozerRadius:10,
  bulldozerSpeed:30,

  // 气垫
  cushionHeight:10,
  cushionLength:25,
  cushionWidth:20,
  cushionMass:20,
  cushionPressure:3000,

  // 多米诺板
  dominoHeight:10,
  dominoCount:12,
  dominoMass:2,
  dominoImpulse:4
}

function saveParams(){
        
  for(var key in params){
    if(key === 'saveParams'){
      continue;
    }
    localStorage.setItem(localStorageKey, JSON.stringify(params));
  }

  console.log('保存参数：', JSON.stringify(params));
}

function clearParams(){
  if(window.confirm('是否清除配置？')){
    if(localStorage){
      // for(var key in localStorage){
      //   localStorage.removeItem(key)
      // }
      localStorage.removeItem(localStorageKey);
      console.log('清除参数成功。');
    }
  }
}

function  loadParams(){
  if(localStorage){
    console.log('加载本地参数：', localStorage.getItem(localStorageKey));
    const json = JSON.parse(localStorage.getItem(localStorageKey));
    for(var key in json){
      if(key === 'saveParams' || key === 'clearParams'){
        continue;
      }
      params[key] = json[key];
    }
  }
}
